package com.sdcqjy.property.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.mode.AttenMode;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.contract.DetailsContract;
import com.sdcqjy.property.presenter.contract.a;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 磁磁帅 on 2018/4/25.
 * 功能：
 */
public class DetailsPresenter extends BaseContract.Presenter<DetailsContract.View> {
    public DetailsPresenter(DetailsContract.View baseView) {
        super(baseView);
    }

    public void getAtten(String id) {
        LoginMode mode = LoginMode.getMode(AppLL.getAppLL());
        Map<String, Object> map = new HashMap<>();
        map.put("documentID", id);
        map.put("userID", "" + mode.id);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.IsAtten, new BaseRequestBack<BaseRet<AttenMode>>() {
                    public BaseRet<AttenMode> jsonToMode(String jsonStr) {
                        BaseRet<AttenMode> ret = new Gson().fromJson(jsonStr, BaseRet.class);
                        if (ret.success) {
                            BaseRet<List<AttenMode>> retList = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<AttenMode>>>() {
                            }.getType());
                            List<AttenMode> data = retList.data;
                            if (data == null || data.size() == 0) {
                                ret.data = null;
                            } else {
                                ret.data = data.get(0);
                            }
                        }
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<AttenMode> ret) {
                        if (baseView != null) {
                            //baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.getAttenRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            //baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        //if (baseView != null) baseView.openLoadDialog();
    }

    public void getDetailsMode(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.GetDetails, new BaseRequestBack<BaseRet<ListMsgMode>>() {
                    public BaseRet<ListMsgMode> jsonToMode(String jsonStr) {
                        BaseRet<List<ListMsgMode>> retList = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<ListMsgMode>>>() {
                        }.getType());
                        BaseRet<ListMsgMode> ret = new BaseRet<>();
                        ret.success = retList.success;
                        ret.code = retList.code;
                        ret.msg = retList.msg;
                        if (ret.success) {
                            List<ListMsgMode> data = retList.data;
                            if (data != null && data.size() > 0) ret.data = data.get(0);
                        }
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<ListMsgMode> ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.getDetailsModeRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void sendRead(String id) {
        LoginMode mode = LoginMode.getMode(AppLL.getAppLL());
        Map<String, Object> map = new HashMap<>();
        map.put("documentID", id);
        map.put("isRead", "1");
        map.put("userID", "" + mode.id);
        if (mode.id == 0) return;
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.Atten, new BaseRequestBack<BaseRet>() {
                    public BaseRet jsonToMode(String jsonStr) {
                        return null;
                    }

                    public void onResponseSucc(BaseRet ret) {
                    }

                    public void onResponseFail(String retStr) {
                    }
                });
    }

    public void sendAtten(String id, int atten) {
        LoginMode mode = LoginMode.getMode(AppLL.getAppLL());
        Map<String, Object> map = new HashMap<>();
        map.put("documentID", id);
        map.put("isAttention", "" + atten);
        map.put("userID", "" + mode.id);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.Atten, new BaseRequestBack<BaseRet>() {
                    public BaseRet jsonToMode(String jsonStr) {
                        return new Gson().fromJson(jsonStr, BaseRet.class);
                    }

                    public void onResponseSucc(BaseRet ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.sendAttenRet(atten);
                            } else baseView.applyError(ret.msg);
                        }

                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void getReadNumb(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("documentID", id);
        new HttpClient().setHttpType(HttpClient.HttpType.GET).setParam(map)
                .httpRequest(a.ReadNumb, new BaseRequestBack<BaseRet<Integer>>() {
                    public BaseRet<Integer> jsonToMode(String jsonStr) {
                        return new Gson().fromJson(jsonStr, new TypeToken<BaseRet<Integer>>() {
                        }.getType());
                    }

                    public void onResponseSucc(BaseRet<Integer> ret) {
                        if (baseView != null) {
                            if (ret.isOk()) {
                                baseView.getReadNumbRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.applyError(retStr);
                        }
                    }
                });
    }

    public void sendEmail(String id) {
        //{"userID":"51","documentID":"11"}
        LoginMode mode = LoginMode.getMode(AppLL.getAppLL());
        Map<String, Object> map = new HashMap<>();
        map.put("documentID", id);
        map.put("userID", "" + mode.id);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.SendEmail, new BaseRequestBack<BaseRet>() {
                    public BaseRet jsonToMode(String jsonStr) {
                        return new Gson().fromJson(jsonStr, BaseRet.class);
                    }

                    public void onResponseSucc(BaseRet ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.sendEmailSucc();
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }
}
